#!/usr/bin/env python
# encoding: utf-8
"""
#-------------------------------------------------------------------#
#                   CONFIDENTIAL --- CUSTOM STUDIOS                 #     
#-------------------------------------------------------------------#
#                                                                   #
#                   @Project Name : Globallawonline                #
#                                                                   #
#                   @File Name    : Findtrafile.py                      #
#                                                                   #
#                   @Programmer   : 李建                            #
#                                                                   #  
#                   @Start Date   : 2021/8/11 15:36                 #
#                                                                   #
#                   @Last Update  : 2021/8/11 15:36                 #
#                                                                   #
#-------------------------------------------------------------------#
# Classes:找翻译后的文件                                                          #
#                                                                   #
#-------------------------------------------------------------------#
"""
import os
import shutil

import pymysql

from Seetings import JGMYSQL_CONNEXT

jg_conn = pymysql.connect(host=JGMYSQL_CONNEXT["MYSQL_HOST"],
                                  port=JGMYSQL_CONNEXT["MYSQL_PORT"],
                                  db=JGMYSQL_CONNEXT["MYSQL_DB"],
                                  user=JGMYSQL_CONNEXT["MYSQL_USER"],
                                  passwd=JGMYSQL_CONNEXT["MYSQL_PASSWD"],
                                  charset=JGMYSQL_CONNEXT["MYSQL_CHARSET"])
DIRS = r'D:\GlobalLawFiles\PDFOUT'
OUT_DIR = r'D:\GlobalLawFiles\全文翻译完毕导出\2021-8-25找的翻译完毕全文\新加坡国际条约翻译完毕全文'
XYZ_OUT_DIR = os.path.join(OUT_DIR, 'XYZ')
YW_OUT_DIR = os.path.join(OUT_DIR, 'YW')
ZW_OUT_DIR = os.path.join(OUT_DIR, 'ZW')
f = open(os.path.join(OUT_DIR, '新加坡国际条约翻译完毕全文.txt'), 'r+')
lines = f.readlines()
f.close()
file_list = []
file_nopdf = []
file_notxt = []
for line in lines:
    file_list.append(line.rstrip(' \n'))
for file in file_list:
    file_name = file.split('/')[-1]
    file_nq = file_name.replace('.pdf', 'nq.txt')
    file_ncq = file_name.replace('.pdf', 'ncq.txt')
    file_neq = file_name.replace('.pdf', 'neq.txt')
    file_dir = '/'.join(file.split('/')[2:-1])
    file_dir_txt = os.path.join(DIRS,file_dir,r'FL\pdfFL\sXMLFL')
    file_dir_pdf = os.path.join(DIRS,file_dir, file_name)
    Is_copy = False
    if not os.path.exists(file_dir_pdf):
        file_nopdf.append(file)
        continue
    for root, dirs, files in os.walk(file_dir_txt):
        if file_nq in files and file_ncq in files and file_neq in files:
            file_nq_oringal = os.path.join(root,file_nq)
            file_nq_target = os.path.join(XYZ_OUT_DIR,file_nq)
            shutil.copyfile(file_nq_oringal, file_nq_target)
            file_ncq_oringal = os.path.join(root, file_ncq)
            file_ncq_target = os.path.join(XYZ_OUT_DIR, file_ncq)
            shutil.copyfile(file_ncq_oringal, file_ncq_target)
            file_neq_oringal = os.path.join(root, file_neq)
            file_neq_target = os.path.join(XYZ_OUT_DIR, file_neq)
            shutil.copyfile(file_neq_oringal, file_neq_target)
            Is_copy = True
            break
        elif file_nq in files and file_ncq in files and file_neq not in files:
            file_nq_oringal = os.path.join(root, file_nq)
            file_nq_target = os.path.join(YW_OUT_DIR, file_nq)
            shutil.copyfile(file_nq_oringal, file_nq_target)
            file_ncq_oringal = os.path.join(root, file_ncq)
            file_ncq_target = os.path.join(YW_OUT_DIR, file_ncq)
            shutil.copyfile(file_ncq_oringal, file_ncq_target)
            Is_copy = True
            break
        elif file_nq in files and file_neq in files and file_ncq not in files:
            file_nq_oringal = os.path.join(root, file_nq)
            file_nq_target = os.path.join(ZW_OUT_DIR, file_nq)
            shutil.copyfile(file_nq_oringal, file_nq_target)
            file_neq_oringal = os.path.join(root, file_neq)
            file_neq_target = os.path.join(ZW_OUT_DIR, file_neq)
            shutil.copyfile(file_neq_oringal, file_neq_target)
            Is_copy = True
            break
    if not Is_copy:
        file_notxt.append(file)
with open(os.path.join(OUT_DIR, '无pdf文件.txt'), "a", encoding='utf-8') as w:
    w.write('\n'.join(file_nopdf))
with open(os.path.join(OUT_DIR, '无txt文件.txt'), "a", encoding='utf-8') as w:
    w.write('\n'.join(file_notxt))
